﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using digit.Core;
using SMS.ServiceManager;
using SMS.UserManager;

public partial class ListCup : WebControlBase
{
    protected int EditIndex = -1;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.lnkDelete.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");
        this.lnkDeleteBottom.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");

        if (this.IsPostBack) return;

        if (ClientState.MemberInfo.UserType != 0 && ClientState.MemberInfo.UserType != 1)
        {
            lnkDelete.Enabled = false;
            lnkDeleteBottom.Enabled = false;
        }
        else
        {
            lnkDelete.Enabled = true;
            lnkDeleteBottom.Enabled = true;
        }

        this.BindData(-1);
    }

    private void BindData(int index)
    {
        try
        {
            DataTable dt = CupManager.SelectAll();
            DataView dv = dt.DefaultView;
            if (txtCupName.Text != "")
                dv.RowFilter = "CupName LIKE '%" + txtCupName.Text.Trim() + "%'";

            grvCup.EditIndex = index;
            grvCup.DataSource = dv;
            grvCup.DataBind();
            if (grvCup.Rows.Count > 0)
            {
                string confirm = "return confirm('Bạn có muốn xóa thông tin này?')";
                for (int i = 0; i < grvCup.Rows.Count; i++)
                {
                    GridViewRow row = grvCup.Rows[i];
                    Button lbtn = (Button)row.FindControl("lnkDelete");
                    if (lbtn != null)
                        lbtn.Attributes.Add("onclick", confirm);

                    DropDownList ddlStat = (DropDownList)row.FindControl("ddlStatus");
                    if (ddlStat != null)
                        ddlStat.SelectedValue = dt.Rows[i]["Status"].ToString();

                    DropDownList ddlEditStat = (DropDownList)row.FindControl("ddlEditStatus");
                    if (ddlEditStat != null)
                        ddlEditStat.SelectedValue = dt.Rows[i]["Status"].ToString();

                    DataTable dtCountry = CountryManager.SelectAll();

                    DropDownList ddlCountry = (DropDownList)row.FindControl("ddlCountry");
                    if (ddlCountry != null)
                    {
                        ddlCountry.DataSource = dtCountry;
                        ddlCountry.DataTextField = "CountryName";
                        ddlCountry.DataValueField = "CountryID";
                        ddlCountry.DataBind();

                        ddlCountry.SelectedValue = dt.Rows[i]["CountryID"].ToString();
                    }

                    DropDownList ddlEditCountry = (DropDownList)row.FindControl("ddlEditCountry");
                    if (ddlEditCountry != null)
                    {
                        ddlEditCountry.DataSource = dtCountry;
                        ddlEditCountry.DataTextField = "CountryName";
                        ddlEditCountry.DataValueField = "CountryID";
                        ddlEditCountry.DataBind();

                        ddlEditCountry.SelectedValue = dt.Rows[i]["CountryID"].ToString();
                    }

                    DropDownList ddlFooter = (DropDownList)grvCup.FooterRow.FindControl("ddlInsCountry");
                    if (ddlFooter != null)
                    {
                        ddlFooter.DataSource = dtCountry;
                        ddlFooter.DataTextField = "CountryName";
                        ddlFooter.DataValueField = "CountryID";
                        ddlFooter.DataBind();
                    }
                }
            }
        }
        catch { }
    }

    protected void grvCup_RowEditing(object sender, GridViewEditEventArgs e)
    {
        EditIndex = e.NewEditIndex;
        BindData(EditIndex);
    }

    protected void grvCup_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        BindData(-1);
    }

    protected void grvCup_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            int id = e.RowIndex;
            grvCup.EditIndex = id;
            GridViewRow row = grvCup.Rows[id];

            string strEditCupName = ((TextBox)row.FindControl("txtEditCupName")).Text;
            byte bytEditStat = byte.Parse(((DropDownList)row.FindControl("ddlEditStatus")).SelectedValue);

            int intCountryID = int.Parse(((DropDownList)row.FindControl("ddlEditCountry")).SelectedValue);

            int intID = int.Parse(grvCup.DataKeys[id].Value.ToString());
            if (intID > 0)
            {
                if (strEditCupName == "")
                {
                    return;
                }
                else
                {
                    CupInfo objInfo = new CupInfo();

                    objInfo.CupID = intID;
                    objInfo.CupName = strEditCupName;

                    if (Session["UserName"] != null)
                        objInfo.CreatedBy = ClientState.MemberInfo.Username;
                    else
                        objInfo.CreatedBy = "";
                    
                    objInfo.CreatedDate = DateTime.Now;
                    objInfo.Status = bytEditStat;
                    objInfo.CountryID = intCountryID;

                    CupManager.Update(objInfo);
                }
            }
            BindData(-1);
        }
        catch { }
    }

    protected void grvCup_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            string commandName = e.CommandName;
            GridViewRow row = grvCup.FooterRow;
            if (commandName == "Insert")
            {
                string strInsCupName = ((TextBox)row.FindControl("txtInsCupName")).Text;
                byte bytInsStat = byte.Parse(((DropDownList)row.FindControl("ddlInsStatus")).SelectedValue);

                if (strInsCupName == "")
                {
                    return;
                }
                else
                {
                    CupInfo objInfo = new CupInfo();

                    objInfo.CupName = strInsCupName;
                    if (Session["UserName"] != null)
                        objInfo.CreatedBy = ClientState.MemberInfo.Username;
                    else
                        objInfo.CreatedBy = "";

                    int intCountryID = int.Parse(((DropDownList)row.FindControl("ddlInsCountry")).SelectedValue);

                    objInfo.CreatedDate = DateTime.Now;
                    objInfo.Status = bytInsStat;
                    objInfo.CountryID = intCountryID;

                    CupManager.Insert(objInfo);
                    BindData(-1);
                }
            }
        }
        catch { }
    }

    protected void grvCup_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            int intID = (int)grvCup.DataKeys[e.RowIndex].Value;
            if (intID != 0)
            {
                CupManager.Delete(intID);
            }
            //Reload.
            BindData(-1);
        }
        catch { }
    }

    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        int iDeleted = 0;

        //Search all checked rows to delete
        foreach (GridViewRow r in this.grvCup.Rows)
        {
            //If checkbox is not checked then quit
            HtmlInputCheckBox cbx = (HtmlInputCheckBox)r.Cells[0].Controls[1];
            if (!cbx.Checked) continue;

            //If checkbox is checked then delete
            CupManager.Delete(Util.ToInteger(cbx.Value));

            iDeleted++;
        }

        //Display result of delete process
        if (iDeleted == 0)
        {
            this.lblError.Text = "Không có bản ghi nào được xóa!";
            return;
        }

        this.lblError.Text = "Đã xóa " + iDeleted.ToString() + " bản ghi!";
        BindData(-1);
    }

    protected void grvCup_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grvCup.PageIndex = e.NewPageIndex;
        this.BindData(-1);
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.BindData(-1);
    }
}
